'''
Test that blog admin is working as intended.
'''

from django.test import TestCase
from django.test.client import Client
from blogs.models import Blog, Post, Tag, Comment
from blogs.forms import CommentForm

class AdminTests(TestCase):
    '''
    These tests will test the functionality of the blog admin tool.
    '''
    
    fixtures = ['blogs.json']
        
    def test_login_required(self):
        response = self.client.get('/blogs/admin/')
        self.assertRedirects(response, '/accounts/login/?next=/blogs/admin/')
        
    def test_index_page(self):
        self.client.login(username='jhsu', password='1234')
        response = self.client.get('/blogs/admin/')
        self.failUnlessEqual(response.status_code, 200)
        
    def test_dashboard(self):
        self.client.login(username='jhsu', password='1234')
        response = self.client.get('/blogs/admin/foo-bar/')
        self.failUnlessEqual(response.status_code, 200)
        
    def test_add_post_continue(self):
        self.client.login(username='jhsu', password='1234')
        
        post_data = {
            'title': 'Adding new post!',
            'slug': 'adding-new-post',
            'body': '<p>Worked!</p>',
            'image': '',
            'tags_str': 'new post',
            'comments_enabled': 'on',
            '_continue': 'Save and Continue',
        }
        
        response = self.client.post('/blogs/admin/foo-bar/post/add/', post_data)
        self.assertRedirects(response, '/blogs/admin/foo-bar/post/adding-new-post/')
        
        post = Post.objects.get(blog='foo-bar', slug='adding-new-post')
        self.assertEquals(post.pub_date, None)
        
    def test_add_post_save(self):
        self.client.login(username='jhsu', password='1234')
        
        post_data = {
            'title': 'Adding new post!',
            'slug': 'adding-new-post',
            'body': '<p>Worked!</p>',
            'image': '',
            'tags_str': 'new post',
            'comments_enabled': 'on',
            '_save': 'Save and Continue',
        }
        
        response = self.client.post('/blogs/admin/foo-bar/post/add/', post_data)
        self.assertRedirects(response, '/blogs/admin/foo-bar/')
        
        post = Post.objects.get(blog='foo-bar', slug='adding-new-post')
        self.assertEquals(post.pub_date, None)
        
    def test_add_post_publish(self):
        self.client.login(username='jhsu', password='1234')
        
        post_data = {
            'title': 'Adding new post!',
            'slug': 'adding-new-post',
            'body': '<p>Worked!</p>',
            'image': '',
            'tags_str': 'new post',
            'comments_enabled': 'on',
        }
        
        response = self.client.post('/blogs/admin/foo-bar/post/add/', post_data)
        self.assertRedirects(response, '/blogs/admin/foo-bar/')
        
        from datetime import datetime
        now = datetime.now()
        post = Post.objects.get(blog='foo-bar', slug='adding-new-post')
        self.assertEquals(post.pub_date.date(), now.date())